import type { AddressItem } from '@/types/global'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAddressStore = defineStore(
  'address',
  () => {
    // 选择地址
    const selectAddress = ref<AddressItem>()

    // 更新地址
    const changeAddress = (address: AddressItem) => {
      selectAddress.value = address
    }
    return { selectAddress, changeAddress }
  },
  // TODO: 持久化
  {
    // 开启数据缓存,只适用于web端
    // persist: true,

    // 以下才是小程序端的正确使用
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
